<?php
use Phalcon\DI\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View\Engine\Volt;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url;
use Phalcon\Cache\Frontend\Output;
use Phalcon\Cache\Backend\File;
use Phalcon\Flash\Session as FlashSession;
use Phalcon\Session\Adapter\Files;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Logger\Adapter\File as FileAdapter;
use Phalcon\Mvc\Model\Manager as ModelsManager;

use Cmf\Library\Acl\Acl;
use Cmf\Plugins\LogPlugin;
use Cmf\Plugins\NotFoundPlugin;

//hello world

//创建依赖注入容器
$di = new FactoryDefault();

//注册路由器:将URI地址解析成Controller\Action\Params的模块
$di->set('router','Phalcon\Mvc\Router');

//注册分发器:利用Router解析得到的信息找到函数并执行
$di->set('dispatcher',function () use ($di) {

	//实例化URL调度控制器
	$dispatcher = new Dispatcher();
	$eventsManager = new EventsManager;
	$eventsManager->attach('dispatch:beforeException',new NotFoundPlugin());

	// //设置调度控制器默认命名空间
	$dispatcher->setDefaultNamespace('Cmf\Controllers');
	$dispatcher->setEventsManager($eventsManager);

	return $dispatcher;
});

//注册响应模块:将执行结果封装成http协议,并发送给浏览器
$di->set('response','Phalcon\Http\Response');

//注册请求模块:将到来的http请求封装成对象,在dispatcher发给controller时也会把对象带过去便于访问
$di->set('request','Phalcon\Http\Request');

//注册数据库模块
$di->set('db',function () use ($config) {
	return new Mysql(array("host" => $config->database->host,"dbname" => $config->database->name,"username" => $config->database->username,"password" => $config->database->password,'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'),));
});

//注册模型事件管理器
$di->set('modelsManager',function () {
	//实例化model管理服务
	$modelsManager = new ModelsManager();
	$eventsManager = new EventsManager();

	//使用日志扩展监听
	$eventsManager->attach('model',new LogPlugin());
	$modelsManager->setEventsManager($eventsManager);

	return $modelsManager;
});

//注册视图视图服务，并指定视图缓存文件存放目录
$di->set('voltService',function ($view,$di) use ($config) {
	$volt = new Volt($view,$di);
	$volt->setOptions(array(
		"compiledPath"      => $config->application->cacheDir.'view/',
		"compiledExtension" => ".compiled"
	));

	if(!file_exists($config->application->cacheDir)) {
		mkdir($config->application->cacheDir.'view/',0777,true);
	}

	return $volt;
});

//注册视图组件，并指定视图文件所在目录，视图不是类文件，无法被autoloader自动加载
$di->set('view',function () use ($config) {
	$view = new View();
	$view->setViewsDir($config->application->viewsDir);
	$view->registerEngines(array('.html' => 'Phalcon\Mvc\View\Engine\Volt','.html' => 'voltService',));

	return $view;
});

//Start the session the first time a component requests the session service
$di->set('session',function () {
	$session = new Files();
	$session->start();

	return $session;
});

$di->set('flash',function () {
	$flash = new FlashSession(array('error' => 'am-alert am-alert-danger','success' => 'am-alert am-alert-success','warning' => 'am-alert am-alert-warning','notice' => 'am-alert',));

	return $flash;
});

$di->set('url',function () use ($config) {
	$url = new Url();
	$url->setBaseUri($config->application->baseUri);
	$url->setStaticBaseUri($config->application->staticUrl);

	return $url;
});

$frontCache = new Output(array("lifetime" => 0,));

$cache = new File($frontCache,array("cacheDir" => "../app/admin/cache/",));

//Register an user component
$di->set('elements',function () {
	return new Elements();
});

//注册权限控制组件
$di->set('acl',function () {
	return new Acl();
});

//注册日志记录组件
$di->set('log',function () use ($config) {
	return new FileAdapter($config->application->logsFileDir.date('Y-m-d').'.log');
});

//注册邮件服务
$di->set('mail',function(){
	return new PHPMailer();
});
